<template>
  <div class="app-container">
    <el-card class="box-card search-div" v-show="showSearch">
        <el-form :inline="true" :model="listQuery" ref="queryForm">
          <el-form-item label="参数名称">
            <el-input v-model="listQuery.configName" :maxlength="100"  clearable></el-input>
            </el-form-item>
         <el-form-item label="参数键名">
            <el-input v-model="listQuery.configKey" :maxlength="100"  clearable></el-input>
         </el-form-item>
         <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
         <el-button @click="handleResetQuery" icon="el-icon-refresh">重置</el-button>
     </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" v-hasPermi="['config_sysConfig_add']" class="el-icon-plus" @click="add">新增</el-button>
        <el-button type="danger" class="el-icon-delete" @click="cleanCache">更新缓存</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>
      <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row class="table-list">
         <el-table-column label="序号" type="index" min-width="5%"/>
              <el-table-column align="center" label="参数名称" prop="configName"/>
             <el-table-column align="center" label="参数键名" prop="configKey"/>
             <el-table-column align="center" label="参数键值" prop="configValue"/>
             <el-table-column align="center" label="备注" prop="remark"/>
             <el-table-column align="center" label="修改时间" prop="updateTime" width="150px"></el-table-column>
             <el-table-column align="center" label="状态" prop="status" width="80px">
               <template slot-scope="scope">
                 <el-tag v-if="scope.row.status==0">正常</el-tag>
                 <el-tag v-if="scope.row.status==1" type="danger">禁用</el-tag>
               </template>
             </el-table-column>
            <el-table-column align="center" label="操作" width="220" fixed="right">
              <template slot-scope="scope">
                <el-button plain size="mini" v-hasPermi="['config_sysConfig_edit']" type="violet" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                 <el-button plain size="mini" type="info" v-hasPermi="['config_sysConfig_enable']" @click="handleStatus(scope.row.id,0)" v-if="scope.row.status==0">停用</el-button>
                 <el-button plain size="mini" type="success" v-hasPermi="['config_sysConfig_enable']" @click="handleStatus(scope.row.id,1)" v-if="scope.row.status==1">启用</el-button>
                <el-button plain size="mini" v-hasPermi="['config_sysConfig_remove']" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
              </template>
          </el-table-column>
      </el-table>
     <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-card>

    <!--弹出框-->
    <el-dialog :title="dialogTitle" :width="'600px'" :visible.sync="showDialog" :close-on-click-modal="false">
      <el-form :rules="rules" ref="savesysConfigData" :model="savesysConfigData" label-width="100px">
        <el-form-item label="参数名称:" prop="configName">
            <el-input v-model="savesysConfigData.configName" :maxlength="100"  placeholder="最多输入100个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="参数键名:" prop="configKey">
            <el-input v-model="savesysConfigData.configKey" :maxlength="100"  placeholder="最多输入100个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="参数键值:" prop="configValue">
            <el-input type="textarea" rows="3" v-model="savesysConfigData.configValue" :maxlength="500"  placeholder="最多输入500个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="备注:" prop="remark">
            <el-input type="textarea" rows="3" v-model="savesysConfigData.remark" :maxlength="500"  placeholder="最多输入500个字符" clearable show-word-limit></el-input>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSave('savesysConfigData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as sysConfig from './js/config'

export default {
  name: 'sysConfigList',
  data() {
    return {
      list: null,
      // 遮罩层
      listLoading: true,
      // 总条数
      total: 0,
      // 显示搜索条件
      showSearch: true,
      listQuery: {
        page: 1, limit: 10,   configName: '',  configKey: '',           },

  showDialog: false,
      savesysConfigData: {  id: '',  configName: '',  configKey: '',  configValue: '',  remark: '', },
      rules: {
        configName: [{ required: true, message: '请输入参数名称', trigger: 'blur' }],
        configKey: [{ required: true, message: '请输入参数键名', trigger: 'blur' }],
        configValue: [{ required: true, message: '请输入参数键值', trigger: 'blur' }],
      },
      dialogTitle: '添加系统参数'
    }
  },
  created() {
  	this.init()
    this.getList()
  },
  methods: {
    // 字典列表初始
    init(){},
    // 字典值列表显示
    getList() { // 获取所有系统参数
      this.listLoading = true
      sysConfig.list(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleResetQuery(){
      this.resetForm("queryForm")
      this.handleSearch()
    },
    //删除
    handleDelete(id) {
      this.$confirm('请确定是否删除该数据', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        sysConfig.removeByIds(id).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 新增
    add(pit, index) {
      this.showDialog = true
      this.dialogTitle = '添加系统参数'
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('savesysConfigData')
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.showDialog = true
      this.dialogTitle = '编辑系统参数'
      this.$nextTick(() => {
        this.resetForm('savesysConfigData')
         // 需要拷贝一下，不然会清空列表
        let tmpRow = JSON.parse(JSON.stringify(row));
        this.savesysConfigData = tmpRow
      })
    },
    // 清理缓存
    cleanCache(){
      sysConfig.cleanCache().then((response) => {
        this.$message({type: 'success',message: '操作成功!'})
        this.getList()
      }).catch(function(error) {
            console.log(error)
      })
    },
    // 启/禁用
    handleStatus(id, type) {
      var status = 1
      var msg = '请确定是否禁用该数据?'
      if (type === 1) {
        status = 0; msg = '请确定是否启用该数据?'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        sysConfig.updateStatusById(id, status).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          sysConfig.editSave(this.savesysConfigData).then((response) => {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getList()
          	this.showDialog = false
          }).catch(function(error) {
            console.log(error)
          })
        }
      })
    }
  }
}
</script>
